cd "C:\Users\creto\Desktop\chinese_students_2017\Submissions\ReStat\Replication Files 2022"

clear
set more off

cap log close


*load data
	use ./kswxy_data, clear

	drop if citycode==.

*set controls
global controls "contract_cons_exw97 chinatariff00_expw97 inputtariff02_expw97 share_revenue_exw97" 
label var contract_cons_exw97 "Contract Intensity"
label var share_revenue_exw97 "Export License"
label var chinatariff00_expw97 "Import Tariffs"
label var inputtariff02_expw97 "Input Tariffs"
label var ln_chinatariff_expw97 "Annual Import Tariffs"
label var ln_inputtariff_expw97 "Annual Input Tariffs"
	
global controls_empwtd90 "contract_cons_empwtd90_isic chinatariff_empwtd90_isic inputtariff_empwtd90_isic share_revenue_empwtd90_isic" 	
	
*Interquartile effect calculation
	sum iv_ntr if year==2013 & balanced==1, de
	local iqr_ivntr = r(p75) - r(p25)
	sum ntrgap_empweighted90 if year==2013 & balanced==1, de
	local iqr_ivntremp90 = r(p75) - r(p25)
	eststo clear
	
//PANEL A//	
	*main result column 1
	eststo A1: reg shc_stud_tot_0213 iv_ntr $controls if year==2013 & balanced==1 , cluster(citycode)
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)

	*drop 4 large cities column 2	
	eststo A2: reg shc_stud_tot_0213 iv_ntr $controls if year==2013 & balanced==1 & largeprefecture==0 , cluster(citycode)
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)

	*drop capitals and coastal cities column 3
	eststo A3: reg shc_stud_tot_0213 iv_ntr $controls if year==2013 & balanced==1 & capital==0 & coastal==0, cluster(citycode)
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	
	*pop weights
	eststo A4: reg shc_stud_tot_0213 iv_ntr $controls [aw=pop05] if year==2013 & balanced==1, cluster(citycode) 
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	
	
	*region fe column 4
	eststo A5: reg shc_stud_tot_0213 iv_ntr $controls i.region if year==2013 & balanced==1, cluster(citycode) 
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	
	*ariel's time changing tariff control column 1
	eststo A6: reg shc_stud_tot_0213 iv_ntr $controls chinatariff0013_expw97 if year==2013 & balanced==1 , cluster(citycode)
	estadd ysumm
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)

//PANEL B: ROBUSTNESS EXERCISES WITH EMPLOYMENT WEIGHTS

	*main result column 1
	eststo E1: reg shc_stud_tot_0213 ntrgap_empwtd90_isic $controls_empwtd90 if year==2013 & balanced==1 , cluster(citycode)
	estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)

	*drop 4 large cities column 2	
	eststo E2: reg shc_stud_tot_0213 ntrgap_empwtd90_isic $controls_empwtd90 if year==2013 & balanced==1 & largeprefecture==0 , cluster(citycode)
	estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)

	*drop capitals and coastal cities column 3
	eststo E3: reg shc_stud_tot_0213 ntrgap_empwtd90_isic $controls_empwtd90 if year==2013 & balanced==1 & capital==0 & coastal==0, cluster(citycode)
	estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)
	
	*region fe column 4
	eststo E4: reg shc_stud_tot_0213 ntrgap_empwtd90_isic $controls_empwtd90 i.region if year==2013 & balanced==1, cluster(citycode) 
	estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)
	
	*ariel's time changing tariff control column 1
	eststo E5: reg shc_stud_tot_0213 ntrgap_empwtd90_isic $controls_empwtd90 chinatariff0013_expw97 if year==2013 & balanced==1 , cluster(citycode)
	estadd ysumm
	estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)
	
	
//PANEL C: MIGRATION//	
	
	*control for change in mig ratio 2005-15 column 1
	eststo B1: reg shc_stud_tot_0213 iv_ntr ch_ratio_skout_0015 ch_ratio_unout_0015 $controls if year==2013 & balanced==1, cluster(citycode)
	estadd ysumm
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	
	*control for change in mig shares 2005-15 column 2
	eststo B2: reg shc_stud_tot_0213 iv_ntr ch_sharemig_sk_0015 ch_sharemig_un_0015 $controls if year==2013 & balanced==1, cluster(citycode)
	estadd ysumm
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	
	*change in ratios and shares 2005-15 column 3
	eststo B3: reg shc_stud_tot_0213 iv_ntr ch_ratio_skout_0015 ch_ratio_unout_0015 ch_sharemig_sk_0015 ch_sharemig_un_0015 $controls if year==2013 & balanced==1, cluster(citycode)
	estadd ysumm
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	 
	*total population (rural + urban) 2005 in denominator column 4
	eststo B4: reg shc_stud_tot_pop05_0213 iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
	
	*population (urban) 2013 in denominator
	eststo B5: reg shc_stud_tot_0213_pop13 iv_ntr $controls if year==2013 & balanced==1 , cluster(citycode)
	estadd ysumm
	estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)


		esttab A1 A2 A3 A4 A5 using "./table4.tex", replace b(3) se(3) ///
		   keep(iv_ntr) label substitute(\_ _) nonotes nocons star(* 0.10 ** 0.05 *** 0.01) stats(iqreffect N r2, ///
		   label("\textit{Interquartile Effect:}\\ \textit{$\Delta$ Students per 1m Pop.}"  "Obs." "R2") fmt(%12.0fc  %12.0fc %12.3fc)) prefoot("\smallskip \\") postfoot("")  ///
		   mtitles("\shortstack{Main Effect\\Col 5 of Table 3}" "\shortstack{Drop 4\\Largest Cities}" "\shortstack{Drop Capital\\Coastal Cities}" "\shortstack{Control for\\Region FE}" "\shortstack{Control for\\Changing Tariffs}")  ///
		  varlabels(, blist(iv_ntr "\it{\underline{A: Robustness Checks}} \vspace{0.1in} \\ ")) booktabs 
		   
		esttab E1 E2 E3 E4 E5 using "./table4.tex", append b(3) se(3) ///
		   keep(ntrgap_empwtd90_isic) nocons nonotes nonumbers label nomtitles substitute(\_ _) star(* 0.10 ** 0.05 *** 0.01) stats(iqreffect N r2, ///
		   label("\textit{Interquartile Effect:}\\ \textit{$\Delta$ Students per 1m Pop.}"  "Obs." "R2 \vspace{0.2in} ") fmt(%12.0fc  %12.0fc %12.3fc)) prehead("") posthead("\bigskip \\") prefoot("\smallskip \\") postfoot("") ///
			varlabels(, blist(ntrgap_empwtd90_isic "\it{\underline{B: Results with Employment Weighted PNTR}} \vspace{0.1in} \\ ")) booktabs  

		esttab B1 B2 B3 B4 B5 using "./table4.tex", append b(3) se(3) ///
		   keep(iv_ntr) nocons nonotes label substitute(\_ _) star(* 0.10 ** 0.05 *** 0.01) stats(iqreffect N r2, ///
		   label("\textit{Interquartile Effect:}\\ \textit{$\Delta$ Students per 1m Pop.}"  "Obs." "R2") fmt(%12.0fc  %12.0fc %12.3fc))  prehead("\midrule") prefoot("\smallskip \\")  ///
		   mtitles("\shortstack{Control for\\In-Migration}" "\shortstack{Control for\\Out-Migration}" "\shortstack{Control for\\In- and Out-\\Migration}" "\shortstack{Total Population\\in Denominator}" "\shortstack{2013 Population\\in Denominator} ")  ///
			varlabels(, blist(iv_ntr "\it{\underline{C: Internal Migration Checks}} \vspace{0.1in}  \\ ")) booktabs
	
	
	
**NOT REPORTED IN TABLE: ROTEMBERG CHECK	

	sum ntrgap_rotem if year==2013 & balanced==1, de
	local iqr_ivntrrotem = r(p75) - r(p25)
	eststo clear
		eststo rotem: reg shc_stud_tot_0213 ntrgap_rotem $controls if year==2013 & balanced==1, cluster(citycode)
		estadd local iqreffect = round(`iqr_ivntrrotem'*_b[ntrgap_rotem]*1000,1)
		
		esttab rotem, b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) stats(iqreffect N) keep(ntrgap_rotem) nocons nonotes 
